package com.sky.controller.user;


import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "C端-地址相关接口")
@RestController
@RequestMapping("/user/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;


    /*
    * 查询当前登录用户的所有地址信息
    * */
    @ApiOperation("查询当前登录用户的所有地址信息")
    @GetMapping("/list")
    public Result<List<AddressBook>>list(){
        //获得地址簿的实体类对象
        AddressBook addressBook = new AddressBook();
        //通过拦截器获得用户id 并封装进地址簿对象中
        addressBook.setUserId(BaseContext.getCurrentId());
        //调用service层方法，把addressBook传入过去
        //查询出来的数据是多条。所有List集合封装
        List<AddressBook>list =addressBookService.list(addressBook);
        //返回数据给前端
        return Result.success(list);
    }



    /*
    * 新增地址
    * */
    @ApiOperation("新增地址")
    @PostMapping
    public Result save(@RequestBody AddressBook addressBook){
        //调用Service层把数据传递过去
        addressBookService.save(addressBook);
        return Result.success();

    }



    /*
    * 根据id查询地址
    * */
    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id){
        //调用Service层方法，把id传入进去根据id查询，
        AddressBook addressBook =addressBookService.getById(id);
        //把数据返回给前端
        return Result.success(addressBook);
    }


    /*
    * 根据id修改地址
    *
    * */
    @ApiOperation("根据id修改地址")
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        //调用Service层方法，把数据传入。
        addressBookService.update(addressBook);
        return Result.success();
    }



    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook) {
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    /**
     * 根据id删除地址
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteById(Long id) {
        addressBookService.deleteById(id);
        return Result.success();
    }

    /**
     * 查询默认地址
     */
    @GetMapping("default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefault() {
        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(1);
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(addressBook);

        if (list != null && list.size() == 1) {
            return Result.success(list.get(0));
        }

        return Result.error("没有查询到默认地址");
    }

}
